home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / KADFILE.ZIP / KAD.C < prev    next >
C/C++ Source or Header  |  1995-03-06  |  10KB  |  217 lines

  1. /****************************************************************************/
  2. /* KAD file routines                                                        */
  3. /* Functions under DOS4Gw and PmodeW                                        */
  4. /*--------------------------------------------------------------------------*/
  5. /* Coded by Kodiak of The Apollo Project                                    */
  6. /* AKA Charles Jones                                                        */
  7. /*     1122 s 32nd St #2                                                    */
  8. /*     Omaha, NE 68105                                                      */
  9. /*     (402)-346-8974                                                       */
  10. /*                                                                          */
  11. /* Email: CAD@UnOmaha.edu                                                   */
  12. /* IRC  : #Coders                                                           */
  13. /*                                                                          */
  14. /*                                                                          */
  15. /* Copyright 1995 All Rights Reservered                                     */
  16. /* Released to public domain, but hey if you use it greet me.               */
  17. /****************************************************************************/
  18. #include  <stdio.h>
  19. #include  <stdlib.h>
  20. #include  "lzc.h"              /* Compression system */
  21. #include  "dir.stc"            /* KADfiles directory structure */
  22. #include  "encrypt.h"
  23.  
  24. #define   MAXDIRS 10           /* Maximum # of Directory Entries. */
  25.                                /* You should minimize this value. */
  26.                                /* If the actual directory size is */
  27.                                /* larger an Error message will inform you */
  28.  
  29.  
  30. dir_struc directory[MAXDIRS];  /* KADfile's internal directory */
  31. FILE      *kadfile;            /* KADfile's handle variable */
  32. long int  numdirs;             /* Number of files in KADfile */
  33. char      altstr[13];          /* Filename for error routines */
  34. char      pathfile[50];        /* Buffer for Path+filename */
  35.  
  36. long int  kadfileinuse;        /* Flag 0=standard files, 1=Kadfile */
  37. long int  currentkadx;         /* Current point to Directory entry */
  38.  
  39. /****************************************************************************/
  40. /* Function : openkadfile                                                   */
  41. /* IN : File name                                                           */
  42. /* RETURNS:                                                                 */
  43. /*          0 = no error                                                    */
  44. /*          2 = file not found                                              */
  45. /*          3 = not enough memory for directory                             */
  46. /****************************************************************************/
  47. /* Note :                                                                   */
  48. /*      This routine will simply return if passed "None" for the filename   */
  49. /*         which indicates that "true files" will be used, not a KADfile    */
  50. /****************************************************************************/
  51. int openkadfile(char *filename, char *password)
  52. {
  53.    int error, x, y, extra;
  54.  
  55.    kadfileinuse = strcmp(filename,"None");
  56.    if (!kadfileinuse)
  57.        return(0);
  58.  
  59.    strcpy(altstr,filename);
  60.    
  61.    extra = 0;
  62.    
  63.    error = 2;
  64.    kadfile = fopen(filename,"r+b");
  65.    if (strstr(filename,".exe") != NULL) 
  66.        extra = 1;
  67.        
  68.    if (kadfile)
  69.    {
  70.       error = 0;
  71.       fseek(kadfile, -sizeof(long int), SEEK_END);
  72.       fread(&numdirs, sizeof(long int), 1, kadfile);
  73.       fseek(kadfile, -(sizeof(dir_struc)*numdirs) - sizeof(long int)-extra, SEEK_END);
  74.       numdirs--;
  75.       strcpy(altstr,"");
  76.       if (numdirs > MAXDIRS)
  77.           error = 3;
  78.       else
  79.           {
  80.              fread(&directory, sizeof(dir_struc)*numdirs, 1, kadfile);
  81.              encryptdir((char *) directory, numdirs, password);
  82.           }
  83.    }
  84.    return(error);
  85. }
  86.  
  87.  
  88. /****************************************************************************/
  89. /* Function : loadfile                                                      */
  90. /* IN : Size of the file to load, and where to put it.                      */
  91. /* RETURNS:                                                                 */
  92. /*          0 = no error                                                    */
  93. /*          3 = file read error                                             */
  94. /****************************************************************************/
  95. /* Note : Loads a file from disk to a specified memory location.            */
  96. /****************************************************************************/
  97. int loadfile(int filesize,char *target)
  98. {
  99.     if (fread(target, filesize, 1, kadfile) == 1)
  100.         return(0);
  101.     return(5);
  102. }
  103.  
  104.  
  105. /****************************************************************************/
  106. /* Function : handleloads                                                   */
  107. /* IN : filename, memory location to load to, filesize                      */
  108. /* RETURNS:                                                                 */
  109. /*          result of appropiate routine                                    */
  110. /*          6 = Unknown file type                                           */
  111. /****************************************************************************/
  112. /* Note : This is the guts of this whole thing.  Add any new files types to */
  113. /*        this function. If you attemp to load an unknown file type and     */
  114. /*        error will result.  This is the only routine you should need to   */
  115. /*        change.                                                           */
  116. /****************************************************************************/
  117. int handleloads(char *filename, char *target, long int size)
  118. {
  119.     if (strstr(filename,".dat") != NULL)
  120.         return(loadfile(size,target)); /* straight file */
  121.     if (strstr(filename,".ari") != NULL)
  122.         return(decompress(target,size)); /* compressed file */
  123.     return(6);
  124. }
  125.  
  126.  
  127. /****************************************************************************/
  128. /* Function : seekfile                                                      */
  129. /* IN : filename                                                            */
  130. /* RETURNS:                                                                 */
  131. /*          0 : No error                                                    */
  132. /*          1 : File not found withing KADfile                              */
  133. /*          4 : Standard file not found in specified path                   */
  134. /****************************************************************************/
  135. /* Note : This function locates and opens the requested file.               */
  136. /****************************************************************************/
  137. int seekfile(char *filename)
  138. {
  139.     strcpy(altstr,filename);              /* record filename for later */
  140.  
  141.     if (!kadfileinuse) {                  /* Standard files */
  142.        if (kadfile)
  143.            fclose(kadfile);
  144.        kadfile = fopen(filename,"r+b");
  145.        if (kadfile)
  146.            return(0);
  147.        return(4);
  148.     }
  149.  
  150.     currentkadx = 0;                      /* Scan internal directory */
  151.     while ((strcmp(directory[currentkadx].name,filename))
  152.             && (currentkadx <= numdirs)) 
  153.                 currentkadx++;
  154.         
  155.     if (currentkadx > numdirs)
  156.        return(1);
  157.     else {
  158.         fseek(kadfile, directory[currentkadx].filepos, 0);
  159.         return(0);
  160.     }
  161. }
  162.  
  163. /****************************************************************************/
  164. /* Function : filesize                                                      */
  165. /* IN : nothing                                                             */
  166. /* RETURNS: filesize of the current file.                                   */
  167. /****************************************************************************/
  168. /* Note : Works for standard files only, file must be opened when called.   */
  169. /****************************************************************************/
  170. int filesize()
  171. {
  172.    long int temp1, temp2;
  173.    temp1 = ftell(kadfile);
  174.    fseek(kadfile,0,2);
  175.    temp2 = ftell(kadfile);
  176.    fseek(kadfile,temp1,0);
  177.    return(temp2);
  178. }
  179.  
  180. /****************************************************************************/
  181. /* Function : getfile                                                       */
  182. /* IN : Path, Filename, pointer to storage                                  */
  183. /* RETURNS:                                                                 */
  184. /*          Returns errors from subprocesses.                               */
  185. /****************************************************************************/
  186. /* Note : Main function for your use.                                       */
  187. /****************************************************************************/
  188. int getfile(char *path, char *filename, char *target)
  189. {
  190.     int error;
  191.  
  192.     if ((error = seekfile(filename)))
  193.        return(error);
  194.  
  195.     if (!kadfileinuse) {
  196.        strcpy(pathfile,path);
  197.        strcat(pathfile,filename);
  198.        return(handleloads(pathfile,target,filesize()));
  199.     }
  200.  
  201.     return(handleloads(filename,target,directory[currentkadx].filesize));
  202. }
  203.  
  204. /****************************************************************************/
  205. /* Function : closekadfile                                                  */
  206. /* IN : nothing                                                             */
  207. /* RETURNS:                                                                 */
  208. /*          Nothing                                                         */
  209. /****************************************************************************/
  210. /* Note : Hey, if I have to explain this one, your in trouble!              */
  211. /****************************************************************************/
  212. void closekadfile()
  213. {
  214.     if (kadfile)
  215.         fclose(kadfile);
  216. }
  217.